package com.ttg.utils.connection;

//~--- JDK imports ------------------------------------------------------------

import java.util.*;

//~--- classes ----------------------------------------------------------------

/**
 * Class HttpClientUtils
 * Description
 * Create 2016-02-25 01:04:41 
 * @author Ardy    
 */
public class HttpClientUtils {

    /**
     * Method sendPostHttpRequest 
     * Description 说明：
     *
     * @param url 说明：
     * @param method 说明：
     * @param param 说明：
     *
     * @return 返回值说明：
     */
    public static String sendPostHttpRequest(String url, String method, Map param) {
        HttpRequest request = new HttpRequest(url);

        request.setParameters(param);
        request.setMethod(( method == null ) ? "POST" : method);

        String     responseStr = "";
        HttpClient httpClient  = null;

        try {
            httpClient  = HttpClient.get();
            responseStr = httpClient.requestWithStringResult(request, 1, false);
        } catch (TTGHttpClientException e) {
            e.printStackTrace();
        }

        return responseStr;
    }

    /**
     * Method sendPostDataHttpRequest 
     * Description 说明：
     *
     * @param url 说明：
     * @param method 说明：
     * @param param 说明：
     *
     * @return 返回值说明：
     */
    public static String sendPostDataHttpRequest(String url, String method, Map param) {
        HttpRequest request = new HttpRequest(url);

        request.setParameters(param);
        request.setMethod(( method == null ) ? "POST" : method);

        String     responseStr = "";
        HttpClient httpClient  = null;

        try {
            httpClient  = HttpClient.get();
            responseStr = httpClient.requestStrWithStringResult(request, 1, false);
        } catch (TTGHttpClientException e) {
            e.printStackTrace();
        }

        return responseStr;
    }

    /**
     * Method createLinkString 
     * Description 说明：
     *
     * @param params 说明：
     *
     * @return 返回值说明：
     */
    public static String createLinkString(Map<String, String> params) {
        List<String> keys = new ArrayList<String>(params.keySet());

        Collections.sort(keys);

        StringBuilder sb    = new StringBuilder();
        String        key   = null;
        String        value = null;

        for (int i = 0 ; i < keys.size() ; ++i) {
            key   = keys.get(i);
            value = params.get(key);
            sb.append(key).append("=").append(value);

            if (i < keys.size() - 1) {
                sb.append("&");
            }
        }

        return sb.toString();
    }

    /**
     * Method createLinkStringQ 
     * Description 说明：
     *
     * @param params 说明：
     *
     * @return 返回值说明：
     */
    public static String createLinkStringQ(Map<String, String> params) {
        List<String> keys = new ArrayList<String>(params.keySet());

        Collections.sort(keys);

        StringBuilder sb    = new StringBuilder();
        String        key   = null;
        String        value = null;

        for (int i = 0 ; i < keys.size() ; ++i) {
            key   = keys.get(i);
            value = params.get(key);
            sb.append(key).append("=").append("\"").append(value).append("\"");

            if (i < keys.size() - 1) {
                sb.append("&");
            }
        }

        return sb.toString();
    }

    /**
     * Method paramFilter 
     * Description 说明：
     *
     * @param sArray 说明：
     * @param signTypeParam 说明：
     * @param signParam 说明：
     *
     * @return 返回值说明：
     */
    public static Map<String, String> paramFilter(Map<String, String> sArray, String signTypeParam, String signParam) {
        Map<String, String> result = new HashMap<String, String>();

        if (( sArray == null ) || ( sArray.size() <= 0 )) {
            return result;
        }

        for (String key : sArray.keySet()) {
            String value = sArray.get(key);

            if (( value == null )
                    || value.equals("")
                    || key.equalsIgnoreCase(signParam)
                    || key.equalsIgnoreCase(signTypeParam)) {
                continue;
            }

            result.put(key, value);
        }

        return result;
    }

    /**
     * Method main 
     * Description 说明：
     *
     * @param args 说明：
     *
     * @throws TTGHttpClientException 异常：
     */
    public static void main(String[] args) throws TTGHttpClientException {}
}


//~ Formatted by Jindent --- http://www.jindent.com
